﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PMS.XCMG.Util
{
    public static class EFFunction
    {
        public static int DateDiff(this DbFunctions _, string datePart, string start, string end) => throw new NotSupportedException();
    }
    public class EFFunctionsTranslator : IMethodCallTranslator
    {
        private readonly ISqlExpressionFactory _sqlExpressionFactory;
        public EFFunctionsTranslator(ISqlExpressionFactory sqlExpressionFactory)
        {
            _sqlExpressionFactory = sqlExpressionFactory;
        }
        private static readonly MethodInfo _DateDiffMethod = typeof(EFFunction).GetMethod(nameof(EFFunction.DateDiff), new[] { typeof(DbFunctions), typeof(string), typeof(string), typeof(string) });
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
        {
            if (method == _DateDiffMethod)
            {
                return _sqlExpressionFactory.NiladicFunction(instance, "DATEDIFF", false, false, typeof(int));
            }
            return null;
        }
    }
}
